package cn.yyt.yytravel.base.security;

import cn.yyt.yytravel.base.response.JsonResult;
import cn.yyt.yytravel.user.mapper.UserMapper;
import cn.yyt.yytravel.user.pojo.vo.UserVO;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Autowired
    UserMapper userMapper;
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        //获取用户信息
        String username = authentication.getName();
        UserVO vehicleUser = userMapper.selectByUsername(username);
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        //返回给前端
        httpServletResponse.getWriter().write(JSON.toJSONString(JsonResult.ok(vehicleUser)));
    }
}